// 定义一些所有组件都需要用到的 公共方法
import wx from "weixin-webview-jssdk"
export default {
  // 动态设置 提示信息的颜色 icon 信息内容
  timestampToTime: function (timestamp) {
    if(timestamp=='' || timestamp == null || timestamp ==undefined){
      return ''
    }
    var date = new Date(timestamp)
    var Y = date.getFullYear()
    var M = (date.getMonth() + 1)
    var D = date.getDate()
    var h = date.getHours()
    var m = date.getMinutes()
    var s = date.getSeconds()
    M = M < 10 ? '0' + M : M
    D = D < 10 ? '0' + D : D
    return Y +'-'+ M +'-'+ D
  },
  timestampToTimeMils: function (timestamp) {
    if(timestamp=='' || timestamp == null || timestamp ==undefined){
      return ''
    }
    var date = new Date(timestamp)
    var Y = date.getFullYear()
    var M = (date.getMonth() + 1)
    var D = date.getDate()
    var h = date.getHours()
    var m = date.getMinutes()
    var s = date.getSeconds()
    M = M < 10 ? '0' + M : M
    D = D < 10 ? '0' + D : D
    h = h < 10 ? '0' + h : h
    m = m < 10 ? '0' + m : m
    s = s < 10 ? '0' + s : s
    return Y +'-'+ M +'-'+ D +' '+ h +':'+ m +':'+ s
  },

  timestampToTimecn: function (timestamp) {
    if(timestamp=='' || timestamp == null || timestamp ==undefined){
      return ''
    }
    var date = new Date(timestamp)
    var Y = date.getFullYear()
    var M = (date.getMonth() + 1)
    var D = date.getDate()
    var h = date.getHours()
    var m = date.getMinutes()
    var s = date.getSeconds()
    M = M < 10 ? '0' + M : M
    D = D < 10 ? '0' + D : D
    return Y +'年'+ M + '月'+ D
  },

  routePush(name, param){
    window.vm .$router.push({
      name: name,
      param: param
    })
  },
  replacePush(name, param){
    window.vm .$router.replace({
      name: name,
      param: param
    })
  },
  ismobile(val) {
    var sReg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;
    var reg = new RegExp(sReg, "i");
    return reg.test(val);
  },
  isphone(val) {
    var sReg = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;
    var reg = new RegExp(sReg, "i");
    return reg.test(val);
  },
  isMoney(money) {
    var reg = /(^[1-9]([0-9]+)?(.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9].[0-9]([0-9])?$)/
    if (reg.test(money)) {
      return true
    } else {
      return false
    }
  },

  turnUpload(fileList){
    if (fileList.length>0){
      for (let i=0;i<fileList.length;i++){
        if (fileList[i].response != undefined){
          fileList[i].file=fileList[i].response.data.url
          fileList[i].fileName=fileList[i].response.data.name
          fileList[i].fileSux=fileList[i].response.data.suffix
        }

      }
    }

  },
  toFixed2(v){
    if (!v){
      return ''
    }
    return Number(v).toFixed(2)
  },
  toFixed1(v){
    if (!v){
      return ''
    }
    return Number(v).toFixed(1)
  },

  parseFilePath(fileList){
    if (!fileList){
      return
    }
    let fileLists = []
    for (let i=0;i<fileList.length;i++){
      if (fileList[i].response){
        // 新增时
        fileLists.push(fileList[i].response.data)
      } else {
        // 修改时
        fileLists.push(fileList[i])
      }

    }
    return fileLists
  },

  parseFilePathSign(file){
    if (!file){
      return
    }
    if (file.response){
      // 新增时
      return file.response.data
    } else {
      // 修改时
      return file
    }
  }

}
